package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Class;
import com.zyg.service.ClassService;
import com.zyg.util.LayData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/class")
public class ClassController {
    @Resource
    private ClassService classService;
    @RequestMapping("/tolist")
    public String tolist(){
        return "class/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public String list(Class class1, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        // 查询数据
        LayData layData = new LayData();
        QueryWrapper<Class> qw = new QueryWrapper<>();
        qw.like(class1.getClassName()!=null&&!"".equals(class1.getClassName()),"class_name",class1.getClassName());
        long count = classService.count(qw);
        layData.setCount((int)count);//强制转换
        //查询List
        Page<Class> page1= new Page<>(page,limit);
        layData.setData(classService.list(page1,qw));
        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);
        return data;
    }//不能根据班级名称查询
/* @RequestMapping("/list")//不能显示年级名称
    @ResponseBody
    public String list(Class class1,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        LayData layData = new LayData();
        QueryWrapper<Class> qw = new QueryWrapper<>();

        // 根据班级名称构建查询条件
        if (class1.getClassName()!= null &&!"".equals(class1.getClassName())) {
            qw.like("class_name", class1.getClassName());
        }

        long count = classService.count(qw);
        layData.setCount((int) count);

        // 使用分页查询获取包含关联年级信息的班级列表
        Page<Class> pageData = new Page<>(page, limit);
        IPage<Class> resultPage = classService.page(pageData, qw);
        List<Class> list = resultPage.getRecords();

        layData.setData(list);
        layData.setCode(0);
        layData.setMsg("查询成功");
        String data = JSON.toJSONString(layData);
        return data;
    }
    */

    @RequestMapping("/toadd")
    public String add(){
        return "class/add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Class class1){
        boolean result = classService.save(class1);
        if(result){
            return JSON.toJSONString(new LayData(0,"添加成功"));
        }else {
            return JSON.toJSONString(new LayData(500,"添加失败"));
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String update(@PathVariable("id") Integer id, Model model){
        Class class1 = classService.getById(id);
        model.addAttribute("class", class1);
        return "class/update";
    }
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Class class1){
        boolean result = classService.updateById(class1);
        if(result){
            return JSON.toJSONString(new LayData(0,"修改成功"));
        }else {
            return JSON.toJSONString(new LayData(500,"修改失败"));
        }
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deletedept(@PathVariable("id") Integer id){
        boolean result = classService.removeById(id);
        if(result){
            return JSON.toJSONString(new LayData(0,"删除成功"));
        }else {
            return JSON.toJSONString(new LayData(500,"删除失败"));
        }
    }
    @RequestMapping("/toinfo/{id}")
    public String toInfo(@PathVariable("id") int id, Model model) {
        Class clazz = classService.getById(id);
        model.addAttribute("clazz", clazz);
        return "class/info";
    }

}
